package controller

import (
	"appService/src/application"
	"appService/src/application/app/appModels"
	"appService/src/application/request"
	"appService/src/authValid"
	"appService/src/authValid/dataAuth"
	"appService/src/global"
	"appService/src/orm"
	"fmt"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gfile"
	"path/filepath"
	"xorm.io/builder"
)

//Public 公共的API接口
type Public struct {
	application.BaseController
}

func (this *Public) fileAuthValidEcho(r *ghttp.Request, allDownload bool, isBranch bool) {
	req := new(request.Request)
	req.FileInit(r)
	fileSign := r.GetString("file_sign", "")
	isAuth := false // 如果这个值为false,那么就输出 没有权限或文件不存在 图片
	//-------
	resFile := &appModels.Files{
		FileName: "您没有权限访问.png",
		FileExt:  "png",
		FileUrl:  filepath.Join(global.ApplicationDir, "/config/image/no_auth.png"),
	}
	if fileSign != "" {
		file := new(appModels.Files)
		ok, err := orm.Db.Where(builder.Eq{"file_sign": fileSign}).Get(file)
		if err == nil {
			if ok {
				folder := authValid.GetFolderRoot(file.FolderId)
				if folder != nil {
					file.FileUrl = filepath.Join(global.FileRootDir, file.FileUrl)
					file.FileName = fmt.Sprintf("%v.%v", file.FileName, file.FileExt)
					if folder.Visit > dataAuth.PUBLIC {
						if req.ValidToken(true) {
							if req.LoginUser.Account.AccountType == dataAuth.SYSTEM {
								isAuth = true
								resFile = file
							} else {
								/*
								ok = false
								if isBranch && config.RolesBranchOpen {
									bids := authValid.GetBranchIDs(req.LoginUser,true)
									for _, bid := range *bids {
										if bid == folder.Bid {
											ok = true
											break
										}
									}
								} else {
									ok = true
								}
								if ok && authValid.FolderCheckUser(folder, req.LoginUser) {
									isAuth=true
									resFile = file
								}
								*/
								if authValid.FolderCheckUser(folder, req.LoginUser) {
									isAuth = true
									resFile = file
								}
							}
						}
					} else {
						isAuth = true
						resFile = file
					}
				}
			} else {
				isAuth = false
				resFile = &appModels.Files{
					FileName: "文件不存在.png",
					FileExt:  "png",
					FileUrl:  filepath.Join(global.ApplicationDir, "/config/image/no_file.png"),
				}
			}
		}
	} else {
		isAuth = false
		resFile = &appModels.Files{
			FileName: "文件不存在.png",
			FileExt:  "png",
			FileUrl:  filepath.Join(global.ApplicationDir, "/config/image/no_file.png"),
		}
	}
	if !gfile.Exists(resFile.FileUrl) {
		isAuth = false
		resFile = &appModels.Files{
			FileName: "文件不存在.png",
			FileExt:  "png",
			FileUrl:  filepath.Join(global.ApplicationDir, "/config/image/no_file.png"),
		}
	}
	if !isAuth {
		allDownload = false
	}
	if allDownload {
		r.Response.ServeFileDownload(resFile.FileUrl, resFile.FileName)
	} else {
		_, ok := global.EchoFileExt[resFile.FileExt]
		if ok {
			r.Response.ServeFile(resFile.FileUrl)
		} else {
			r.Response.ServeFileDownload(resFile.FileUrl, resFile.FileName)
		}
	}
}

//FileDownLoad 文件下载 任何文件都使用下载模式
func (this *Public) FileDownLoad(r *ghttp.Request) {
	this.fileAuthValidEcho(r, true, true)
}

//File 文件下载 配置了输出的文件就使用下载模式
func (this *Public) File(r *ghttp.Request) {
	this.fileAuthValidEcho(r, false, true)
}
